$(function () {
    layui.use('table', function () {
        var table = layui.table

        table.render({
            elem: '#topic-table',
            url: '/TopicManager/getTopicList?' + $("#searchform").serialize(), //数据接口
            page: true,
            method: 'post',
            contentType: 'application/json;charset=utf-8',
            cols: [
                [
                    {type: 'checkbox', fixed: 'left', width: 50},
                    {field: 'name', title: '题目名称', width: '15%'},
                    {field: 'type', title: '题型', width: '10%'},
                    {field: 'kname', title: '知识点', width: '17%'},
                    {field: 'modifyName', title: '最后修改人	', width: '10%'},
                    {
                        field: 'modifyDate', title: '最后修改时间', width: '17%', templet: function (d) {
                            return getTimeStr(d.modifyDate)
                        }
                    },
                    {
                        field: 'difficulty', title: '难度', width: '8%', templet: function (d) {
                            switch (d.difficulty) {
                                case '0':
                                    return '低难度'
                                case '1':
                                    return '中难度'
                                case '2':
                                    return '高难度'
                                default:
                                    return ''
                            }
                        }
                    },
                    {
                        field: 'state', title: '状态', width: '10%', templet: function (d) {
                            return d.state === 'enabled' ? '启用' : '停用'
                        }
                    },
                ],
            ],
            parseData: parseLimitResult,
        })

        function getTimeStr(data) {
            return String(data.year + 1900) + '/' + data.month + '/' + data.date + ' ' + data.hours + ':' + data.minutes + ':' + data.seconds
        }

        $('#searchbtn').on('click', function () {
            tableReload($('#searchfrom').serializeArray().reduce(function (total, cur) {
                total[cur.name] = cur.value;
                return total
            }, {}))
        })

        function tableReload(search) {
            table.reload('topic-table', {
                where: {
                    search: search
                }
            })
        }

        function parseLimitResult(value) {
            return {
                "code": value.status - 1,
                "msg": value.msg,
                "count": value.page.total,
                "data": value.data,
            }
        }

        $('#topic_add').on('click', function () {
            location = "/TopicManager/addTopicFrame/"
        })

        $('#topic_edit').on('click', function () {
            var data = getTopicId()
            if (!data) {
                return
            }
            location = "/TopicManager/addTopicFrame/?id=" + data[0].id
        })

        $('#topic_view').on('click', function () {
            var data = getTopicId()
            if (!data) {
                return
            }
            layer.open({
                type: 1,
                title: '查看题目',
                content: $('#view-page'),
                offset: '30px',
                area: ['800px', '600px']
            })

            //题干
            $('#view-page > table:nth-child(1) > tbody > tr:nth-child(1) > td:nth-child(2)').html(data[0].data)

            //选项
            var options = $('#view-page > table:nth-child(1) > tbody > tr:nth-child(2)')
            if (data[0].type === '单项选择题' || data[0].type === '不定项选择题') {
                var trs = $('#view-page > table:nth-child(1) > tbody tr')
                trs.slice(2, -2).remove()
                trs.show()
                var opttests = JSON.parse(data[0].options).map(function (a, b) {
                    return String.fromCharCode(b + 65) + '. ' + a
                })
                options.find('td').eq(0).attr('rowspan', opttests.length)
                options.find('td').eq(1).html(opttests[0])
                for (var index = 1; index < opttests.length; index++) {
                    var n = index + 1
                    $('#view-page > table:nth-child(1) > tbody > tr:nth-child(' + n + ')').after('<tr><td>' + opttests[index] + '</td></tr>')
                }
            } else {
                $('#view-page > table:nth-child(1) > tbody tr').slice(1, -2).hide()
            }
            //答案
            if (data[0].type === '单项选择题' || data[0].type === '不定项选择题' || data[0].type === '判断正误题') {
                $('#view-page > table:nth-child(1) > tbody > tr:nth-last-child(2) > td:nth-child(2)').html(data[0].choose)
                $('#view-page > table:nth-child(1) > tbody > tr:nth-last-child(2)').show()
            } else {
                $('#view-page > table:nth-child(1) > tbody > tr:nth-last-child(2)').hide()
            }
            //解析
            $('#view-page > table:nth-child(1) > tbody > tr:nth-last-child(1) > td:nth-child(2)').html(data[0].answer)


            $('#view-page > table:nth-child(2) > tbody > tr:nth-child(1) > td:nth-child(2)').html(data[0].type)
            $('#view-page > table:nth-child(2) > tbody > tr:nth-child(1) > td:nth-child(4)').html(data[0].state === 'enabled' ? '启用' : '停用')
            $('#view-page > table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(2)').html(data[0].name)
            $('#view-page > table:nth-child(2) > tbody > tr:nth-child(2) > td:nth-child(4)').html(data[0].path)
            $('#view-page > table:nth-child(2) > tbody > tr:nth-child(3) > td:nth-child(2)').html(data[0].modifyName)
            $('#view-page > table:nth-child(2) > tbody > tr:nth-child(3) > td:nth-child(4)').html(data[0].modifyDate.time)
        })

        $('#topic_ena').on('click', function () {
            var data = getTopicId()
            if (!data) {
                return
            }
            var state = data[0].state !== 'enabled'
            var id = data[0].id
            $.ajax({
                url: '/TopicManager/enableTopic',
                data: {'id': id, 'state': state},
                success: function () {
                    tableReload()
                }
            })
        })

        $('#topic_del').on('click', function () {
            var data = getTopicId()
            if (!data) {
                return
            }
            $.ajax({
                url: '/TopicManager/deleteTopic?id=' + data[0].id,
                success: function () {
                    tableReload()
                    layer.msg('删除成功')
                }
            })
        })

        $('#topic_import').on('click', function () {
            layer.open({
                type: 1,
                title: '批量导入题目',
                content: $('#import-page'),
                offset: '30px',
                area: ['600px', '500px']
            })
        })
        $('#import-page > div').on('click', function () {
            var formData = new FormData()
            formData.append('upfile', $('input[type=file][name=file]')[0].files[0])
            $.ajax({
                url: '/TopicManager/importTopic',
                type: 'post',
                data: formData,
                contentType: false,
                processData: false,
                success: function (result) {
                    console.log(result)
                    layer.msg('上传成功')
                    tableReload()
                },
                error: function () {
                    layer.msg('上传失败')
                }
            })
        })

        function getTopicId() {
            let data = table.checkStatus('topic-table').data;
            if (data.length !== 1) {
                layer.msg('请选择一个题目');
                return null;
            }
            console.log(data)
            return data
        }
    })
})
